<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div>
        <!-- 搜索 -->
        <el-input v-model="query.ryxx" clearable size="small" placeholder="姓名、身份证" style="width: 200px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <el-date-picker v-model="query.kfrq" type="date" value-format="yyyyMMdd" format="yyyyMMdd" placeholder="选择开方日期" class="filter-item" style="width: 150px;" @change="crud.toQuery" />
        <el-select v-model="query.sndj" clearable size="small" placeholder="选择失能等级" class="filter-item" style="width: 150px" @change="crud.toQuery">
          <el-option
            v-for="item in sndjTypeOptions"
            :key="item.key"
            :label="item.display_name"
            :value="item.key"
          />
        </el-select>
        <rrOperation :crud="crud" />
      </div>
      <div class="crud-opts">
        <span class="crud-opts-left">
          <!--左侧插槽-->
          <slot name="left" />
          <el-button
            class="filter-item"
            size="mini"
            type="primary"
            icon="el-icon-plus"
            @click="openFixedCostsMaster()"
          >
            记 账
          </el-button>
          <!--右侧-->
          <slot name="right" />
        </span>
      </div>
      <!--表单组件-->
      <el-dialog :visible.sync="masterDialog" title="批量批费" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-row>
            <el-col :span="8">
              <el-form-item label="全局固费" prop="masterName">
                <el-select
                  v-model="masterName"
                  style="width: 270px"
                  placeholder="请选择"
                  @change="seletChange"
                >
                  <el-option
                    v-for="item in allFixedCostsMaster"
                    :key="item.bisFixedCostsMasterId"
                    :label="newLable(item.bisFixedCostsMasterName, item.bisFixedCostsMasterAmount)"
                    :value="item.bisFixedCostsMasterId"
                  />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button :loading="this.fullscreenLoading" @click="masterDialog = false">取消</el-button>
          <el-button :loading="this.fullscreenLoading" type="primary" @click="exePlgf">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="localselectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column align="center" prop="xm" label="姓名" />
        <el-table-column align="center" prop="sfz" label="身份证" />
        <el-table-column align="center" prop="cblx" label="参保类型" />
        <el-table-column align="center" prop="sndj" label="失能登记" />
        <el-table-column align="center" prop="ryrq" label="入院日期" />
        <el-table-column align="center" prop="kfrq" label="开方日期" />
        <el-table-column align="center" prop="ljfy" label="累计费用" />
        <el-table-column align="center" prop="ts" label="住院天数" />
        <el-table-column align="center" prop="tczf" label="统筹支付" />
        <el-table-column align="center" prop="xjzf" label="现金支付" />
        <el-table-column align="center" prop="bl" label="自付比例" />
        <el-table-column align="center" prop="jsResult" label="结算结果" />
      </el-table>
    </div>
  </div>
</template>

<script>
import crudJzdj from '@/api/business/jzdj'
import { queryAllCostsMaster } from '@/api/business/fixedCostsMaster'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'

const defaultForm = { bisJzdjId: null, bisJzdjZhsqId: null, bisJzdjLsh: null, bisJzdjYllb: null, bisJzdjRyrq: null, bisJzdjCyrq: null, bisJzdjRyjbbm: null, bisJzdjGhf: null, bisJzdjZcf: null, bisJzdjCyjbbm: null, bisJzdjJbr: null, bisJzdjJbrq: null, bisJzdjFph: null, bisJzdjCyzt: null, bisJzdjSpr: null, bisJzdjZcyymc: null, bisJzdjKsmc: null, bisJzdjBs: null, bisJzdjCwh: null, bisJzdjYsxm: null, bisJzdjHzlxfs: null, bisJzdjJslxfs: null, bisJzdjSpkssj: null, bisJzdjSpzzsj: null, ywlx: null, lrr: null, lrrxm: null, lrbm: null, lrbmmc: null, lrsj: null, gxr: null, gxrxm: null, gxbm: null, gxbmmc: null, gxsj: null, sfyx: null, bz: null }
export default {
  name: 'Jzdj',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: '就诊登记', url: 'api/jzdj/queryJzdjResultVo', idField: 'bisJzdjId', sort: 'bisJzdjId,desc', crudMethod: { ...crudJzdj }})
  },
  data() {
    return {
      sndjTypeOptions: [
        { key: '中度失能', display_name: '中度失能' },
        { key: '重度失能', display_name: '重度失能' },
        { key: '85周岁以上', display_name: '85周岁以上' },
        { key: '90周岁以上', display_name: '90周岁以上' }
      ],
      allFixedCostsMaster: [],
      masterName: '',
      masterId: '',
      masterDialog: false,
      masterKfrq: '',
      masterSndj: '',
      masterRyxx: '',
      masterIds: [0],
      fullscreenLoading: false,
      permission: {
        add: ['admin', 'jzdj:add'],
        edit: ['admin', 'jzdj:edit'],
        del: ['admin', 'jzdj:del']
      },
      rules: {
        bisJzdjRyrq: [
          { required: true, message: '入院日期不能为空', trigger: 'blur' }
        ],
        bisJzdjKsmc: [
          { required: true, message: '科室名称不能为空', trigger: 'blur' }
        ],
        bisJzdjBs: [
          { required: true, message: '病室不能为空', trigger: 'blur' }
        ],
        bisJzdjCwh: [
          { required: true, message: '床位号不能为空', trigger: 'blur' }
        ],
        bisJzdjYsxm: [
          { required: true, message: '医生姓名不能为空', trigger: 'blur' }
        ],
        bisJzdjHzlxfs: [
          { required: true, message: '患者联系方式不能为空', trigger: 'blur' }
        ],
        bisJzdjSpkssj: [
          { required: true, message: '审批开始时间不能为空', trigger: 'blur' }
        ],
        bisJzdjSpzzsj: [
          { required: true, message: '审批终止时间不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.queryAllCostsMaster()
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    // 选择改变
    localselectionChangeHandler(val) {
      this.crud.selections = val
      this.masterIds = [0]
      console.log(val)
      val.forEach((item, index) => {
        console.log(item)
        this.masterIds.push(item.bisJzdjId)
      })
    },
    openFixedCostsMaster() {
      console.log(this.query.kfrq)
      console.log(this.masterIds)
      if (this.query.kfrq === '' || this.query.kfrq === undefined) {
        this.$notify({
          title: '请选择开方日期',
          type: 'warning',
          duration: 2500
        })
        return false
      }

      let flag = false
      let t_ryrq = ''
      let t_xm = ''
      if (this.masterIds.length === 1 && this.masterIds[0] === 0) {
        this.crud.data.forEach(data => {
          t_ryrq = data.ryrq
          t_xm = data.xm
          flag = this.query.kfrq < t_ryrq
        })
        if (flag) {
          this.$notify({
            title: '开方日期[' + this.query.kfrq + ']不可以小于人员[' + t_xm + ']入院日期[' + t_ryrq + ']',
            type: 'warning',
            duration: 2500
          })
          return false
        }
      } else {
        this.crud.selections.forEach((item, index) => {
          t_ryrq = item.ryrq
          t_xm = item.xm
          flag = this.query.kfrq < t_ryrq
        })
        if (flag) {
          this.$notify({
            title: '开方日期[' + this.query.kfrq + ']不可以小于选中人员[' + t_xm + ']入院日期[' + t_ryrq + ']',
            type: 'warning',
            duration: 2500
          })
          return false
        }
      }
      this.masterKfrq = this.query.kfrq
      this.masterSndj = this.query.sndj
      this.masterRyxx = this.query.ryxx
      this.masterDialog = true
    },
    // 获取所有机构服务项目
    queryAllCostsMaster() {
      queryAllCostsMaster().then(res => {
        this.allFixedCostsMaster = res
      }).catch(() => { })
    },
    // 获取选中的对象
    seletChange(val) {
      // 选中的数据和options进行匹配
      var obj = {}
      obj = this.allFixedCostsMaster.find(function(i) {
        console.log('val:', val)
        return i.bisFixedCostsMasterId === val
      })
      const that = this
      // 在change中获取到整条对象数据
      console.log(obj)
      that.masterId = obj.bisFixedCostsMasterId
    },
    // 获取选中的对象
    newLable(val1, val2) {
      return val1 + '(' + val2 + '元)'
    },
    /**
     * 批量过方
     * @param index
     * @param rows
     */
    exePlgf() {
      if (this.masterId === '') {
        this.$notify({
          title: '请选择全局固费',
          type: 'warning',
          duration: 2500
        })
        return
      }
      const msgTip = this.masterSndj === undefined ? '' : '失能等级为[' + this.masterSndj + ']，'
      const len = (this.masterIds.length === 1 && this.masterIds[0] === 0) ? this.crud.data.length : this.masterIds.length - 1
      this.$confirm('开方日期为[' + this.masterKfrq + ']，' + msgTip + '最终在院人员为' + len + '人。', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.fullscreenLoading = true
        const param = { 'ids': this.masterIds,
          ryxx: this.masterRyxx === undefined ? '' : this.masterRyxx,
          sndj: this.masterSndj === undefined ? '' : this.masterSndj,
          kfrq: this.masterKfrq,
          masterId: this.masterId }
        console.log('param:', param)
        crudJzdj.exePlgf(param)
          .then(res => {
            this.fullscreenLoading = false
            this.masterDialog = false
            this.$notify({
              title: '已过费',
              type: 'success',
              duration: 2500
            })
            console.log(res)
            this.crud.toQuery()
          })
          .catch(err => {
            this.fullscreenLoading = false
            this.$notify({
              title: err,
              type: 'warning',
              duration: 2500
            })
          })
      })
    }
  }
}
</script>

<style>
.crud-opts {
  padding: 4px 0;
  display: -webkit-flex;
  display: flex;
  align-items: center;
}
.crud-opts .crud-opts-right {
  margin-left: auto;
}
.crud-opts .crud-opts-right span {
  float: left;
}
</style>
